package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.DeptDao;
import com.itheima.domain.system.Dept;
import com.itheima.factory.DaoInstanceFactory;
import com.itheima.service.system.DeptService;

import java.util.List;
import java.util.UUID;

public class DeptServiceImpl implements DeptService {

    @Override
    public PageInfo<Dept> findAll(String sizeStr, String pageStr) {
        int page = 1;
        try {
            page = Integer.parseInt(pageStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        int size = 5;
        try {
            size = Integer.parseInt(sizeStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        //设置分页助手
        PageHelper.startPage(page, size);
        DeptDao dao = DaoInstanceFactory.getMapper(DeptDao.class);
        List<Dept> list = dao.findAll();
        PageInfo<Dept> dept = new PageInfo<>(list);
        return dept;
    }

    @Override
    public void save(Dept dept) {
        DeptDao dao = DaoInstanceFactory.getMapper(DeptDao.class);
        String id = UUID.randomUUID().toString().replace("-", "");
        dept.setId(id);
        dao.save(dept);

    }

    @Override
    public List<Dept> findAll() {
        DeptDao mapper = DaoInstanceFactory.getMapper(DeptDao.class);
        List<Dept> list = mapper.findAll();
        return list;
    }

    @Override
    public Dept findById(String id) {
        DeptDao dao = DaoInstanceFactory.getMapper(DeptDao.class);
        Dept dept = dao.findById(id);
        return dept;
    }

    @Override
    public void update(Dept dept) {
        DeptDao dao = DaoInstanceFactory.getMapper(DeptDao.class);
        dao.update(dept);
    }

    @Override
    public void delete(String id) {
        DeptDao dao = DaoInstanceFactory.getMapper(DeptDao.class);
        dao.delete(id);
    }
}
